package com.xmxsuperstar.app.client.twitter;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import net.love5z.common.json.JSONArray;
import net.love5z.common.json.JSONException;
import net.love5z.common.json.JSONObject;
import android.util.Log;

import com.xmxsuperstar.app.client.twitter.model.TwitterStatus;
import com.xmxsuperstar.app.client.twitter.model.TwitterUser;

public class TwitterRetweetedTimeLineClient extends TwitterTimeLineClient {

    private static String LogTag = "TwitterRetweetedTimeLineClient";

    @Override
    protected List<TwitterStatus> parase(String content) {
        List<TwitterStatus> list = new ArrayList<TwitterStatus>();

        try {
            JSONArray stArray = new JSONArray(content);
            for (int i = 0; i < stArray.length(); i++) {
                JSONObject st = stArray.getJSONObject(i);

                TwitterStatus status = new TwitterStatus();
                status.setId(st.getLong("id"));
                status.setText(st.getJSONObject("retweeted_status").getString("text"));
                SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy",
                    Locale.ENGLISH);
                sdf.setLenient(false);
                status.setCreatedAt(sdf.parse(st.getString("created_at")));

                TwitterUser user = new TwitterUser();
                user.setId(st.getJSONObject("retweeted_status").getJSONObject("user").getLong("id"));
                user.setName(st.getJSONObject("retweeted_status").getJSONObject("user")
                    .getString("name"));
                user.setProfileImageUrl(new URL(st.getJSONObject("retweeted_status")
                    .getJSONObject("user").getString("profile_image_url")));
                user.setScreenName(st.getJSONObject("retweeted_status").getJSONObject("user")
                    .getString("screen_name"));

                status.setUser(user);

                list.add(status);
            }
        } catch (JSONException e) {
            Log.e(LogTag, e.getMessage());
        } catch (ParseException e) {
            Log.e(LogTag, e.getMessage());
        } catch (MalformedURLException e) {
            Log.e(LogTag, e.getMessage());
        }

        return list;
    }

    @Override
    protected String getBaseUrlStr() {
        return "http://api.twitter.com/1/statuses/retweeted_to_me.json";
    }

    public static void main(String[] args) {
        TwitterRetweetedTimeLineClient client = new TwitterRetweetedTimeLineClient();
        //List<TwitterStatus> list1 = client.getTimeLineWithEntities();
        // System.out.println(list1);
        // List<TwitterStatus> list2 = client.getTimeLineByMaxId(
        // list1.get(list1.size() - 1).getId(), 20);
        //
        // System.out.println(list2);
    }

}
